package com.example.examsystem.model;

import com.example.examsystem.enums.QuestionType;
import jakarta.persistence.*;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 试题实体类
 */
@Data
@Entity
@Table(name = "questions")
@EntityListeners(AuditingEntityListener.class)
public class Question {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "title", nullable = false, length = 500)
    private String title;
    
    @Column(name = "content", columnDefinition = "TEXT")
    private String content;
    
    @Enumerated(EnumType.STRING)
    @Column(name = "type", nullable = false, length = 20)
    private QuestionType type;
    
    @Column(name = "difficulty", nullable = false)
    private Integer difficulty;
    
    @Column(name = "score", nullable = false)
    private Double score;
    
    @Column(name = "subject_id", nullable = false)
    private Long subjectId;
    
    @Column(name = "category_id")
    private Long categoryId;
    
    @Column(name = "options", columnDefinition = "JSON")
    private String options;
    
    @Column(name = "correct_answer", columnDefinition = "TEXT")
    private String correctAnswer;

    @Column(name = "answer", columnDefinition = "TEXT")
    private String answer;
    
    @Column(name = "explanation", columnDefinition = "TEXT")
    private String explanation;
    
    @Column(name = "created_by")
    private Long createdBy;
    
    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;
    
    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
    
    // 关联关系
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "subject_id", insertable = false, updatable = false)
    private Subject subject;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id", insertable = false, updatable = false)
    private Category category;
    
    // 便捷方法
    public void setOptions(List<QuestionOption> optionList) {
        // 这里可以实现JSON序列化逻辑
        // 将选项列表转换为JSON字符串存储
        if (optionList != null && !optionList.isEmpty()) {
            // TODO: 实现JSON序列化，暂时简化处理
            this.options = "[]"; // 占位符
        }
    }

    public List<QuestionOption> getOptionsList() {
        // 这里可以实现JSON反序列化逻辑
        // 将JSON字符串转换为选项列表
        // TODO: 实现JSON反序列化，暂时返回空列表
        return java.util.Collections.emptyList();
    }
    
    /**
     * 试题选项内部类
     */
    @Data
    public static class QuestionOption {
        private String content;
        private boolean correct;
    }
}




